﻿
User = {
    OnUpdateUserClick: function () {
        $('table#Userstbl .btnUdpate').live("click", function (e) {
            e.preventDefault();
            var currentRow = $(this).closest("tr");
            var id = $(this).attr("data-id");
            User.EditRow(currentRow);
        });
    },
    EditRow: function (currentRow) {
        var userId = currentRow.find("td").eq(0).html();
        var Username = currentRow.find("td").eq(1).html();
        var Password = currentRow.find("td").eq(2).html();
        var Email = currentRow.find("td").eq(3).html();
        $('#UserId').val(userId);
        $('#Username').val(Username);
        $('#Password').val(Password);
        $('#Email').val(Email);
        $("#UserForm").show('slide', { direction: 'left' }, 800);
    },
    OnAddUserClick: function () {
        $('#btnAddUser').click(function (e) {
            e.preventDefault();
            User.ClearForm();
            $("#UserForm").show('slide', { direction: 'left' }, 800);
        });
    },
    ClearForm: function () {
        $('#UserId').val('0');
        $('#Username').val('');
        $('#Password').val('');
        $('#Email').val('');
    },
    OnDeleteUserClick: function () {
        $('table#Userstbl .btnDelete').live("click", function (e) {
            e.preventDefault();
            if (confirm('Are you sure you want to delete user?')) {
                var id = $(this).attr("data-id");
                var json = JSON.stringify({ Id: id });
                var currentRow = $(this).closest("tr");
                $.ajax({
                    type: "POST",
                    url: "../Webservices/UserWebService.asmx/DeleteUser",
                    data: json,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        var response = eval('(' + data.d + ')');
                        if (response.Success) {
                            currentRow.remove();
                            alert('Deleted!');
                        }
                    }
                });

            }
        });
    }
}

$(document).ready(function () {
    User.OnUpdateUserClick();
    User.OnAddUserClick();
    User.OnDeleteUserClick();
});